$(function(){
	//点击上传图片
	clickSendImg();
	//发说说
	sendMeg();
});

/**
 * 点击上传图片
 */
function clickSendImg () {
	$("#sendMessageForm #clickSendImg").click(function (){
		$("#imgPhoto").click();
	});
	$("#imgPhoto").change(function (){
		$("#sendImgForm").submit();
	});
}

/**
 * 发说说
 */
function sendMeg(){
	$("#sendMessage button").click(function (){
		var photo = $("#showPhoto img").attr("src");
		var msg = $("#sendMessageForm textarea").val();
		sendMessage(photo,msg);
	});
}

/**
 * 清空textarea 隐藏showphoto
 * @param a
 */
function resultToClear(){
	$("#showPhoto").css("display","none");
	$("#sendMessage textarea").val("");
}

/**
 * 查询说说DIV
 * @param data
 */
function createSaySomethingDiv(a,clear){
	var json = $.parseJSON(a);
	if(clear){
		$("#friendMessage div").remove();
	}
	for(var i=0;i<json.length;i++){
		var div = '<div class="message">'
			+'<img src="/TTchat/images/'+json[i].imgUrl+'" class="friendHeadImg"></img>'
			+'<div class="friendName">'+json[i].name+'</div>'
			+'<div class="friendTime">'+json[i].time+'</div>';
		
		if(json[i].msg != "" && json[i].msg != null){
			div = div +'<div class="friendMsg">'+json[i].msg+'</div>';
		}
		if(json[i].photo != "" && json[i].photo != null){
			div = div +'<div class="friendPhoto">'
			+'<img src="'+json[i].photo+'" height="360" width="360"></img></div>';
		}
		div = div + '<div class="friendGood">';
		if(json[i].good == 1){
			div = div + '<div class="notGood" onClick="giveGood(this)"></div>';
		}else if(json[i].good == 0){
			div = div + '<div class="Good" onClick="giveGood(this)"></div>';
		}
		if(json[i].count != 0){
			div = div + '<div class="goodCount">'+json[i].count+'人觉得很赞!</div>';
		}
		div = div + '</div>'
			+ '<div class="hide">'+json[i].id+'</div>'
			+ '<div class="friendComment">'
			+ '<textarea placeholder="评论"></textarea>'
			+ '<button onClick="sendComment(this)">发表</button>'
			+ '</div>'
			+ '<div class="commentArea">'
			+ '<ul>';
		var haha = json[i].comment.length;
		for(var j=0;j<haha;j++){
			div = div + "<li style='width:672px;word-wrap:break-word'>"
				+ "<span style='font-size:12px'>"+json[i].comment[j].name+" </span>"
				+"<span style='color:#999;font-size:8px'>"+json[i].comment[j].time+": </span>"
				+"<span style='font-size:12px'>"+json[i].comment[j].msg+"</span>"
				+"<span class='hide'>"+json[i].comment[j].cid+"</span>"
				+ "</li>";
		}	
		div = div + '</ul>'
			+ '</div>'
			+ '</div>';
		$("#friendMessage").append(div);
	}
}
/**
 * 点赞/取消点赞
 * @param a
 */
function giveGood(a){
	var id = $(a).parent().next().text();
	if($(a).attr("class") == "notGood"){
		$(a).attr("class","Good");
		removeGoodAjax(id);
		var text = $(a).next().text();
		var index = text.indexOf("人觉得很赞!");
		var count = text.substring(0,index) - 1;
		if(count == 0){
			$(a).next().text("");
		}else{
			text = count + "人觉得很赞!";
			$(a).next().text(text);
		}
		
	}else if($(a).attr("class") == "Good"){
		$(a).attr("class","notGood");
		giveGoodAjax(id);
		var text = $(a).next().text();
		if(text == ""){
			$(a).parent().append("<div class='goodCount'>1人觉得很赞!</div>");
		}else{
			var index = text.indexOf("人觉得很赞!");
			var count = parseInt(text.substring(0,index)) + 1;
			text = count + "人觉得很赞!";
			$(a).next().text(text);
		}
	}
}

/**
 * 发送评论
 */
function sendComment(a){
	var value = $(a).prev().val();
	if(value == ""){
		return false;
	}else{
		var id = $(a).parent().prev().text();
		sendCommentAjax(value,id,a);
	}
}
